import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { map } from "rxjs/operators";
import { saveAs } from 'file-saver';

@Injectable()
export class NoticeService {

    constructor(private http: HttpClient) { }

    /** 加载收件箱通知数据 */
    loadReceiveList(params) {
        return this.http.post('/system/api/notice/loadreceivelist' , params).pipe(
            map((res: Response) => res['data']),
        );
    }
    /** 加载发送箱通知数据 */
    loadPublishList(params) {
        return this.http.post('/system/api/notice/loadpublishlist' , params).pipe(
            map((res: Response) => res['data']),
        );
    }

    /** 加载角色类型 */
    loadRoleData() {
        return this.http.get('/system/api/notice/loadroledata').pipe(
            map((res: Response) => res['data']),
        );
    }

    /** 保存发布的信息 */
    save(params) {
        return this.http.post('/system/api/notice/save' , params).pipe(
            map((res: Response) => res['data']),
        );
    }

    /** 删除发布信息 */
    deleteNoticeById(noticeId) {
        return this.http.get('/system/api/notice/deletenoticebyid?noticeId=' + noticeId).pipe(
            map((res: Response) => res['data']),
        );
    }

    /** 阅读回执 */
    haveRead(noticeId) {
        return this.http.get('/system/api/notice/haveread?noticeId=' + noticeId).pipe(
            map((res: Response) => res['data']),
        );
    }

    /** 获取附件链接地址 */
    getUrlById(params) {
        return this.http.post('/media/api/geturlbyid', params).pipe(
            map((res: Response) => res['data']),
        );
    }

    /** 附件下载 */
    downland(url, name) {
        return this.http.get('/media/api/storage/download?path=' + url, { 'responseType': 'blob' }).pipe(
            map(res => saveAs(res, name)));
    }

    loadNoticeById(noticeId) {
        return this.http.get('/system/api/notice/loadnoticebyid?noticeId=' + noticeId).pipe(
            map((res: Response) => res['data']),
        );
    }

}
